//
//  MoreViewController.m
//  weibo-01
//
//  Created by HZApple on 16/5/9.
//  Copyright © 2016年 hdu. All rights reserved.
//

#import "MoreViewController.h"
#import "ThemeImageView.h"
#import "ThemeLabel.h"
#import "ThemeManager.h"
#import "ThemeChoiceViewController.h"

@interface MoreViewController ()
@property (weak, nonatomic) IBOutlet ThemeImageView *themeImageView;
@property (weak, nonatomic) IBOutlet ThemeLabel *themeChoiceLabel;
@property (weak, nonatomic) IBOutlet ThemeLabel *themeShowLabel;
@property (weak, nonatomic) IBOutlet ThemeImageView *accountImageView;
@property (weak, nonatomic) IBOutlet ThemeLabel *accountLabel;
@property (weak, nonatomic) IBOutlet ThemeImageView *opinionImageView;
@property (weak, nonatomic) IBOutlet ThemeLabel *opnionLabel;
@property (weak, nonatomic) IBOutlet ThemeImageView *clearIamgeView;
@property (weak, nonatomic) IBOutlet ThemeLabel *claerLabel;
@property (weak, nonatomic) IBOutlet ThemeLabel *clearShowLabel;
@property (weak, nonatomic) IBOutlet ThemeLabel *logOutLabel;


@end

@implementation MoreViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadConfig];
    
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleSingleLineEtched];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
     _themeShowLabel.text = [[ThemeManager sharedManager] themeName];
}

- (void)loadConfig {
    [_themeImageView setImageName:@"more_icon_theme"];
    [_clearIamgeView setImageName:@"more_icon_draft"];
    [_opinionImageView setImageName:@"more_icon_feedback"];
    [_accountImageView setImageName:@"more_icon_account"];
    
    _themeChoiceLabel.text = @"主题选择";
    _themeChoiceLabel.colorName = @"More_Item_Text_color";
    
   
    _themeShowLabel.colorName = @"More_Item_Text_color";
    
    _accountLabel.text = @"账户管理";
    _accountLabel.colorName = @"More_Item_Text_color";
    
    _opnionLabel.text = @"意见反馈";
    _opnionLabel.colorName = @"More_Item_Text_color";
    
    _claerLabel.text = @"清空缓存";
    _claerLabel.colorName = @"More_Item_Text_color";
    
    NSInteger i = [self readCatchSize];
    _clearShowLabel.text = [NSString stringWithFormat:@"%.2f MB",i / 1024.0 /1024.0];
    _clearShowLabel.colorName = @"More_Item_Text_color";
    
    _logOutLabel.text = @"注销当前账号";
    _logOutLabel.colorName = @"More_Item_Text_color";
    [self themeChange];
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    
    self = [super initWithCoder:aDecoder];
    
    if (self) {
        //监听主题切换的通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(themeChange) name:kThemeChangeNotification  object:nil];
    }
    
    return self;
}

- (void)themeChange{

    UIColor *backColor = [[ThemeManager sharedManager] themeColorWithColorName:@"More_Item_color"];
    self.tableView.backgroundColor = backColor;
    
    UIColor *lineColor = [[ThemeManager sharedManager] themeColorWithColorName:@"More_Item_Line_color"];
    self.tableView.separatorColor= lineColor;
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark 读取、清空缓存
- (NSInteger)readCatchSize {
    
    NSInteger fileSize = 0;
    
    NSString *catchPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"];
    
    NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:catchPath];
    
    for (NSString *str in fileEnumerator) {
        NSString *filePath = [catchPath stringByAppendingPathComponent:str];
        
        NSDictionary *attDic = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
        
        fileSize += [attDic fileSize];
        
    }
    return fileSize;
}

- (void)removeCatch {
    NSString *catchPath = [NSHomeDirectory() stringByAppendingString:@"/Library/Caches"];
    [[NSFileManager defaultManager] removeItemAtPath:catchPath error:nil];
}
#pragma mark uitableviewdelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        [self.navigationController pushViewController:[[ThemeChoiceViewController alloc] init] animated:YES];
    }
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:kThemeChangeNotification object:nil];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
